package mongorest

import (
	"fmt"
	"net/http"
	"os/user"
	"path"
)

// Auth contains user and password info.
type Auth struct {
	Username string
	Password string
	// TODO(vegacom@gmail.com): enhance security by using a temporal hashed token.
}

// Mongodb 
type Mongodb struct {
	Addr            string
	AuthForDatabase map[string]*Auth
}

type Settings struct {
	BindIP  string
	Port    int
	Mongodb *Mongodb
}

func LoadSettings(settingsFname string) error {
	if settingsFname == "" {
		userinfo, err := user.Current()
		if err != nil {
			return err
		}
		settingsFname = path.Join(userinfo.HomeDir, settingsPath)
	}
	var err error
	settings, err = newSettings(settingsFname)
	return err
}

func PatternAndHandler() (string, http.Handler) {
	return apiPrefix, appHandler(handleAPI)
}

func WebAddr() string {
	return fmt.Sprintf("%s:%d", settings.BindIP, settings.Port)
}

func GetSettings() string {
	return pretty(settings)
}
